package com.bangdream.michelia.presenter;

import android.app.Dialog;
import android.util.Log;

import com.bangdream.michelia.contract.TeacherContract;
import com.bangdream.michelia.entity.TeacherBean;
import com.bangdream.michelia.model.ITeacherModelImpl;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.rejava.BaseObserverEntityNormalHttp;
import com.bangdream.michelia.tool.retrofithttp.TokenManagerDef;
import com.bangdream.michelia.utils.L;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/8/6 22:49
 * @email 909732184@qq.com
 * 讲师服务
 */
public class TeacherPresenter<V> extends BasePresenter<V> {
    private TeacherContract.ITeacherModel model;

    public TeacherPresenter() {
        this.model = new ITeacherModelImpl();
    }

    public void getTeacherList(final int page, int pageSize, Dialog pd) {
        final TeacherContract.ITeacherView view = getView();
        if (view == null)
            return;
        this.pd = pd;
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("pageNo", page + "");
        paramsMap.put("pageSize", pageSize + "");
        TokenManagerDef.work(() -> model.getTeacherList(paramsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<TeacherBean>>(this, pd) {
                    @Override
                    public void onHandleSuccess(List<TeacherBean> teacherBeans) {
                        if (view == null)
                            return;
                        if (teacherBeans != null) {
                            L.d("getTeacherList\n" + teacherBeans.toString());
                        }
                        view.setTeacherList(true, page, teacherBeans, getDescribe(teacherBeans));
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        view.setTeacherList(false, page, null, message);
                    }
                });
    }

    public void getTeacherDetails(String id) {
        final TeacherContract.ITeacherView view = getView();
        if (view == null)
            return;
        TokenManagerDef.work(() -> model.getTeacherDetails(id)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<TeacherBean>(this, null) {
                    @Override
                    public void onHandleSuccess(TeacherBean teacherBean) {
                        if (view == null)
                            return;
                        view.setTeacherDetails(true, teacherBean, getDescribe(teacherBean));
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        view.setTeacherDetails(false, null, message);
                    }
                });
    }

    private TeacherContract.ITeacherView getView() {
        return (TeacherContract.ITeacherView) obtainView();
    }
}
